package com.dmzapp.api.servlet;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.dmzapp.api.cache.redis.RedisClient;
import com.dmzapp.api.db.AppDao;
import com.dmzapp.api.service.RedisAndDBService;
import com.dmzapp.api.vo.IncomeRecord;
import com.dmzapp.api.vo.UserAccount;

public class AddUserCreditHandler extends DMZSerlvet implements javax.servlet.Servlet {

	static final long serialVersionUID = 1L;
    private ObjectMapper mapper = new ObjectMapper();
    
    public static final int SDK_TYPE_WAPS = 1;
    public static final int SDK_TYPE_MIDII = 2;
    public static final int SDK_TYPE_DIANJIN = 3;
    public static final int SDK_TYPE_QUMI = 4;
    
	@Override
	protected void handle(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String deviceid = request.getParameter("d");
		String credit = request.getParameter("credit");
		int offer_type = Integer.valueOf(request.getParameter("offer_type"));
		String username = (String) request.getAttribute("u");
		String packagename = request.getParameter("p");
		String ip = (String) request.getAttribute("i");
		
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		UserAccount useraccount = RedisClient.getInstance().checkUserExist(username);
        if (useraccount == null) {
            useraccount = AppDao.getInstance().getUserInfo(username);
            if (useraccount != null) {
                RedisClient.getInstance().setUseraccount(useraccount);
            }
        }
        
        if (useraccount == null || !deviceid.equals(useraccount.getDeviceid())) {
        	responseMap.put("status", "500");
            responseMap.put("msg", "非法用户");
            return ;
        }
        
//        AppInfo tmpApp = LocalCache.getInstance().appCache.get(packagename);
//        String useTimesStr = RedisClient.getInstance().checkLongtimeuseEffective(username, packagename);
//        int maxTimes = tmpApp.getMax_times();
        
		
        // 综合以上为有效安装，操作数据库插入操作,并写入redis中
        IncomeRecord incomeRecord = new IncomeRecord();
        incomeRecord.setDeviceid(deviceid);
        incomeRecord.setIncome(credit);
        incomeRecord.setPackagename(packagename);
        incomeRecord.setServiceid(2);
        incomeRecord.setUsername(username);
        incomeRecord.setOffer_type(offer_type);
        incomeRecord.setCreate_time(new Date());
        incomeRecord.setIp(ip);
        // 计算该用户的余额
        RedisAndDBService.addUserRestAndAmount(username, credit);
        
        // 记录用户的收入信息
        RedisAndDBService.inserIncomeDetail(incomeRecord);
        
        responseMap.put("amount", RedisAndDBService.getUserAmount(username));
        responseMap.put("rest", RedisAndDBService.getUserRest(username));
        responseMap.put("server_time", Calendar.getInstance().getTimeInMillis());
        responseMap.put("status", "200");
        responseMap.put("msg", "恭喜您，获得体验奖励");
        try {
            mapper.writeValue(response.getWriter(), responseMap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
	}
    
    
}
